function roomNameToObj(room) {
    if (typeof (room) === 'string') room = Game.rooms[room]
    return room
}
function creepNameToObj(creep) {
    if (typeof (creep) === 'string') creep = Game.creeps[creep]
    return creep
}
function objIDToObj(obj) {
    if (typeof (obj) === 'string') obj = Game.getObjectById(obj)
    return obj
}
/**判断房间归属
 * 1. 属于我的房间返回0，无归属的房间返回1，无视野的房间返回2，属于其他人的房间返回3
 * @param {Room} room 
 * @returns 
 */
function roomOwner(room) {
    room = roomNameToObj(room)
    let result = 0
    if (!room) {
        result = 2  // 无视野
    } else {
        room.controller.my ? result = 0 : !room.controller.owner ? result = 1 : result = 3
    }
    return result
}
/**兑换像素点 */
function getPiex() {
    if (Game.cpu.bucket >= 10000) {
        let bucket = Game.cpu.bucket
        try {
            Game.cpu.generatePixel()
            console.log(`当前有${bucket}未使用的CPU数量,已使用${bucket - Game.cpu.bucket}兑换像素点`);
        } catch (error) {
            console.log(`${Date()} :利用多余的的cpu兑换像素点出现异常,跳过本次兑换`);
        }
    }
}
/**获取指定坐标点范围内的地形数据 */
function getCollectPoint(centPos, range = 1, minRadius = 1) {
    // console.log(`当前资源点的坐标和房间编号${this.pos}`);
    let canBuildPos = []
    /**TODO: 通过房间静态数据计算该坐标点指定范围内的地形 */
    const terrain = Game.rooms[centPos.roomName].getTerrain()
    for (let posX = - range; posX <= range; posX++) {
        for (let posY = - range; posY <= range; posY++) {
            if (posX == 0 && posY == 0) continue
            const distance = Math.sqrt(posX * posX + posY * posY);
            if (distance >= minRadius && distance < range + 1) {
                const adjposX = centPos.x + posX
                const adjposY = centPos.y + posY
                if (terrain.get(adjposX, adjposY) != 1) {
                    /** 0	不存在	地形为 plain
                        1	TERRAIN_MASK_WALL	地形为 wall
                        2	TERRAIN_MASK_SWAMP	地形为 swamp */
                    canBuildPos.push({ 'x': adjposX, 'y': adjposY, 'type': terrain.get(adjposX, adjposY) == 0 ? 'plain' : 'swamp' })
                }
            }
        }
    }
    return canBuildPos
}

/**初始化内存处理 */
function resetGameMemory() {
    Object.keys(Memory.creeps).map(creepName => {
        if (!Game.creeps[creepName]) {
            delete Memory.creeps
        }
    })
}


export {
    roomNameToObj,
    creepNameToObj,
    objIDToObj,
    roomOwner,
    getPiex,
    getCollectPoint,
}